<template>
  <div class="friend-req-list">
    <div class="req-head">新的好友</div>
    <scroll-box class="scroll-box" listenHeight ref="scrollBox" :topDisabled="true" :bottomDisabled="bottomDisabled" :bottomDistance="30" @touchBottom="getFriendReqList(friendReqList[friendReqList.length-1].createdAt)">
      <div class="req-list" v-if="friendReqList.length > 0">
        <div class="req-item" v-for="item in friendReqList" :key="item.id">
          <div class="avatar"><img :src="item.user.avatar" alt=""></div>
          <div class="item-info">
            <div class="info-left">
              <div class="name">{{item.user.userName}}</div>
              <div class="content">{{item.content}}</div>
            </div>
            <div class="accept" v-if="item.handle === 0" @click="addFriendFaq(item)">接受</div>
            <div class="accepted" v-else>已添加</div>
          </div>
        </div>
        <loading v-if="!bottomDisabled && friendReqList.length >= 20"></loading>
      </div>
      <div v-else class="tips">暂无好友请求哦</div>
    </scroll-box>
  </div>
</template>

<script>
import { api_getFriendReqList } from '@/api/friendRequest'
import { api_addFriend } from '@/api/friend'
import ScrollBox from '@/components/ScrollBox.vue'
import Loading from '@/components/Loading.vue'

export default {
  components: { ScrollBox, Loading },
  name: 'friend-req-list',
  data () {
    return {
      // 好友请求列表
      friendReqList: [],
      bottomDisabled: false,
    }
  },
  created () {
    this.getFriendReqList()
  },
  methods: {
    // 请求列表数据
    getFriendReqList (lastTime) {
      this.bottomDisabled = true;
      api_getFriendReqList({ lastTime }).then(res => {
        if (res.data.code === 200) {
          this.friendReqList.push(...res.data.list)

          this.bottomDisabled = res.data.list.length < 20;
        }
      })
    },

    // 接受好友请求的回调
    addFriendFaq (item) {
      api_addFriend({ requestId: item.id }).then(res => {
        if (res.data.code === 200) {
          // console.log(res);
          let list = this.$store.state.friendListData.list
          this.$set(list, list.length, res.data.userInfo)
          item.handle = 1;
        }
      })
    }
  }
}
</script>

<style scoped>
.friend-req-list {
  width: 100%;
}

.req-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 55px;
  border-bottom: 1px solid #ddd;
  padding: 0 27px;
  font-size: 18px;
}

.scroll-box {
  height: calc(100vh - 55px);
  overflow: auto;
}

.req-item {
  text-align: left;
  display: flex;
  width: 450px;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  margin: 0 auto;
}

.avatar {
  height: 45px;
  width: 45px;
  min-width: 45px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-info {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  align-items: center;
  width: 100%;
  margin-left: 10px;
}

.content {
  color: rgb(126, 126, 126);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 5px;
}

.accept {
  padding: 5px 11px;
  border-radius: 3px;
  background-color: #1aad19;
  color: white;
  cursor: pointer;
}

.accepted {
  color: rgb(126, 126, 126);
}

.tips {
  margin-top: 50px;
  text-align: center;
  color: rgb(126, 126, 126);
}
</style>